# -*- coding: utf-8 -*-
import cherrypy
import logging
import functools
from hashlib import md5
import time
import datetime
import urllib2, urllib
import json
import random
import os

import config
import views

PWD_ERROR = {"code":203, "msg":"帐号/密码错误"}


def exp(method):
    @functools.wraps(method)
    def wrapper(*args, **kwargs):
        try:
            return method(*args, **kwargs)
        except:
            logging.error("api error: %s" % method.__name__, exc_info=True)
            return {"code":500, "msg":"程序错误,请稍等"}
    return wrapper

class Server:

    @cherrypy.expose
    @cherrypy.tools.json_out()
    def conf(self, cp='', cv='', bid='', uid='', **kwargs):
        return views.conf(cp, cv, bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    def game_des(self, cp='', cv='', bid='', uid='', **kwargs):
        return views.game_des(cp, cv, bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def login(self, cp, cv='', bid='', access_token='', openid='', devid='', login_type='wx', pid='', **kwargs):
        return views.login(cp, cv, bid, access_token, openid, devid, login_type)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def user_info(self, cp, cv='', bid='', uid='', **kwargs):
        return views.get_user_info(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def set_pid(self, cp, cv='', bid='', uid='', pid='', **kwargs):
        return views.set_pid(bid, uid, pid)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def set_signature(self, cp, cv='', bid='', uid='', signature='', **kwargs):
        return views.set_signature(bid, uid, signature)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def bf_create_club(self, cp, cv='', bid='', uid='', pwd='', lat='', lon='', **kwargs):
        return views.bf_create_club(cp, cv, bid, uid, pwd, lat, lon)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def create_club(self, cp, cv='', bid='', uid='', pwd='', name='', province='', city='', head='', tag='',des='', **kwargs):
        return views.create_club(cp, cv, bid, uid, pwd, name, province, city, head, tag, des)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def myclub(self, cp, cv='', bid='', uid='', **kwargs):
        return views.myclub(bid, uid)
    

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def joined_club(self, cp, cv='', bid='', uid='', **kwargs):
        return views.joined_club(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def find_club(self, cp, cv='', bid='', uid='', cid='', tag='', city='', **kwargs):
        return views.find_club(bid, uid, cid, tag, city)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def recommend_club(self, cp, cv='', bid='', uid='', **kwargs):
        return views.recommend_club(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def join_club(self, cp, cv='', bid='', uid='', cid='', **kwargs):
        return views.join_club(bid, uid, cid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def unverify_member(self, cp, cv='', bid='', uid='', cid='', **kwargs):
        return views.unverify_member(bid, cid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def join_verify(self, cp, cv='', bid='', uid='', cid='', member='', verify='', **kwargs):
        return views.join_verify(cid, bid, uid, member, verify)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def club_member(self, cp, cv='', bid='', uid='', cid='', member='', act='list', **kwargs):
        return views.club_member(cid, bid, uid, member, act)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def club_set(self, cp, cv='', bid='', uid='', cid='', name='', head='', des='', bg_pic='', tag='', **kwargs):
        return views.club_set(cid, name, head, des, bg_pic, tag)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def club_info(self, cp, cv='', bid='', uid='', cid='', **kwargs):
        return views.club_info(bid, cid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def create_game(self, cp, cv='', bid='', uid='', cid='', name='', game_type='', player='', all_round='', all_player='', rule='', **kwargs):
        return views.create_game(cid, name, game_type, player, all_round, all_player, rule)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def enroll_game(self, cp, cv='', bid='', uid='', gid='', **kwargs):
        return views.enroll_game(gid, bid, uid)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def joined_rid(self, cp, cv='', bid='', uid='', **kwargs):
        return views.joined_rid(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def quit_club(self, cp, cv='', bid='', uid='', pwd='', cid='', **kwargs):
        return views.quit_club(cid, bid, uid, pwd)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def dismiss_club(self, cp, cv='', bid='', uid='', pwd='', cid='', **kwargs):
        return views.dismiss_club(cid, bid, uid, pwd)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def create_room(self, cp, cv='', bid='', uid='', pwd='', player='', game_type='gdmj', all_round=8, gps_lat='', gps_lon='', rule='', **kwargs):
        return views.create_room(cp, cv, bid, uid, pwd, player, game_type, all_round, gps_lat, gps_lon, rule)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def join_room(self, cp, cv='', bid='', uid='', pwd='', rid='', gps_lat='', gps_lon='', **kwargs):
        return views.join_room(cp, cv, bid, uid, pwd, rid, gps_lat, gps_lon)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def quit_room(self, cp, cv='', bid='', uid='', pwd='', rid='', **kwargs):
        return views.quit_room(cp, cv, bid, uid, pwd, rid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def dismiss_room(self, cp, cv='', bid='', uid='', pwd='', rid='', **kwargs):
        return views.dismiss_room(cp, cv, bid, uid, pwd, rid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    def upload(self, cp, cv='', pwd='', bid='', uid='', up_file='', **kwargs):
        name, ext = os.path.splitext(up_file.filename)
        today = time.strftime("%Y%m%d")
        if ext == '.aac':
            save_path = "./voice/%s/%s/%s/%s" % (bid, today[:4], today[4:6], today[-2:])
        else:
            save_path = "./img/%s/%s/%s/%s" % (bid, today[:4], today[4:6], today[-2:])
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        pic_tag = int(time.time())
        filename = "%s%s" % (pic_tag, ext.lower())
        file_path = "{path}/{file}".format(path=save_path, file=filename)
        f = open(file_path, 'wb+')
        f.write(up_file.file.read())
        f.close()
        if file_path[0] == '.':
            file_path = file_path[1:]
        url = "%s%s" % (config.UPLOAD_ADDR, file_path)
        return {"code":0, "msg":"ok", "data":{"url":url}}
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def feedback(self, bid, uid, content, **kwargs):
        return views.feedback(bid, uid, content)
    
    @cherrypy.expose
    def game_start(self, rid, bid='szjt', **kwargs):
        return views.game_start(rid, bid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def room_log(self, bid='', uid='', **kwargs):
        return views.room_log(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def room_log_detail(self, bid, uid, rid, **kwargs):
        return views.room_log_detail(bid, uid, rid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def id_auth(self, bid, uid, id_no, name, **kwargs):
        return views.id_auth(bid, uid, id_no, name)
    
    
class MatchServer():
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def all_match(self, bid, uid, pwd, **kwargs):
        return views.all_match(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def create(self, bid, uid, pwd, game_type, all_player, start_time, name, des='', **kwargs):
        if not (game_type and all_player and start_time and name):
            return {"code":404, "msg":"选项不能为空,请输入完整"}
        score = 100
        return views.create_match(bid, uid, game_type, score, all_player, start_time, name, des)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def join(self, bid, uid, pwd, mid, pid, **kwargs):
        if not (mid and pid):
            return {"code":404, "msg":"选项不能为空,请输入完整"}
        return views.join_match(bid, uid, mid, pid)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def quit(self, bid, uid, pwd, mid, **kwargs):
        return views.quit_match(bid, uid, mid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def info(self, bid, uid, pwd, mid, **kwargs):
        return views.match_info(bid, uid, mid)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def edit(self, bid, uid, pwd, mid, name='', des='', all_player='', start_time='', **kwargs):
        return views.match_edit(bid, uid, mid, name, des, all_player, start_time)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def mine(self, bid, uid, pwd, **kwargs):
        return views.mine_match(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def joined(self, bid, uid, pwd, **kwargs):
        return views.joined_match(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def list_member(self, bid, uid, pwd, mid, **kwargs):
        return views.list_match_member(bid, uid, mid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def del_member(self, bid, uid, pwd, mid, member, **kwargs):
        return views.del_match_member(bid, uid, mid, member)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def set_manager(self, bid, uid, pwd, mid, member, **kwargs):
        return views.set_match_manager(bid, uid, mid, member)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def unset_manager(self, bid, uid, pwd, mid, member, **kwargs):
        return views.unset_match_manager(bid, uid, mid, member)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def verify(self, bid, uid, pwd, mid, member, verify, **kwargs):
        return views.verify_match_member(bid, uid, mid, member, verify)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def start(self, bid, uid, pwd, mid, **kwargs):
        return views.start_match(bid, uid, mid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def pause(self, bid, uid, pwd, mid, **kwargs):
        return views.pause_match(bid, uid, mid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def stop(self, bid, uid, pwd, mid, **kwargs):
        return views.stop_match(bid, uid, mid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def keep_on(self, bid, uid, pwd, mid, **kwargs):
        return views.keep_match(bid, uid, mid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def revive_apply(self, bid, uid, pwd, mid, member, **kwargs):
        return views.revive_apply(bid, uid, mid, member)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def out_member(self, bid, uid, pwd, mid, **kwargs):
        return views.match_out_member(bid, uid, mid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def result(self, bid, uid, pwd, mid, **kwargs):
        return views.match_result(bid, uid, mid)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def result_detail(self, bid, uid, pwd, mid, mround, **kwargs):
        return views.match_result_detail(bid, uid, mid, mround)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def current_ranking(self, bid, uid, pwd, **kwargs):
        return views.current_ranking(bid, uid)
    